﻿@using NewLife;
@using XCode;
@using XCode.Configuration;
@{
    var entity = Model as IEntity;
    var fields = ViewBag.Fields as FieldCollection;
    var groupFields = fields.GroupByCategory(entity);
}

@if (groupFields.Count > 1)
{
    var i = 0;
    var j = 0;
    <ul class="nav nav-tabs" role="tablist">
        @foreach (var item in groupFields)
        {
            <li class="@(i==0?"active":"")"><a href="@("#item"+i)" data-toggle="tab">@item.Key</a></li>
            i++;
        }
    </ul>
    <div class="tab-content">
        @foreach (var group in groupFields)
        {
            <div class="tab-pane fade in @(j==0?"active":"")" id="@("item"+j)">
                <div class="row">
                    @foreach (var item in group.Value)
                    {
                        if ((!item.PrimaryKey || item.Field != null && !item.Field.IsIdentity) && (item.DataVisible == null || item.DataVisible(entity)))
                        {
                            if (item is FormField formField && !formField.GroupView.IsNullOrEmpty())
                            {
                                @await Html.PartialAsync(formField.GroupView, new ValueTuple<IEntity, DataField>(entity, item))
                            }
                            else
                            {
                                @await Html.PartialAsync("_Form_Group", new ValueTuple<IEntity, DataField>(entity, item))
                            }
                        }
                    }
                </div>
            </div>
            j++;
        }
    </div>
}
else
{
    foreach (var item in fields)
    {
        // 表单页显示非主键或非自增字段
        if ((!item.PrimaryKey || item.Field != null && !item.Field.IsIdentity) && (item.DataVisible == null || item.DataVisible(entity)))
        {
            if (item is FormField formField && !formField.GroupView.IsNullOrEmpty())
                @await Html.PartialAsync(formField.GroupView, new ValueTuple<IEntity, DataField>(entity, item))
            else
                @await Html.PartialAsync("_Form_Group", new ValueTuple<IEntity, DataField>(entity, item))
        }
    }
}
